#coding=utf-8
"""
Author:杭州多测师_周sir
Time:2025/8/28 14:03
website:https://duoceshi.net
"""

'''
此模块是用来封装所有用例的基类
'''
import unittest
from time import *

# 调试代码
# from selenium import webdriver
# driver  = webdriver.Chrome()

class BasePage(unittest.TestCase):

    @classmethod
    def set_driver(cls,driver):
        cls.driver = driver

    @classmethod
    def get_driver(cls):  # 单例设计模式
        return cls.driver

    # baidu_input = ('id',"chat-textarea")
    @classmethod
    def find_element(cls,element):
        type = element[0] # id
        value = element[1] #chat-textarea

        if type == "id":
            elem = cls.driver.find_element_by_id(value)
        elif type == "class":
            elem = cls.driver.find_element_by_class_name(value)
        elif type == "xpath":
            elem = cls.driver.find_element_by_xpath(value)
        elif type == "name":
            elem = cls.driver.find_element_by_name(value)
        elif type == "css":
            elem = cls.driver.find_element_by_css_selector(value)
        elif type == "link_text":
            elem = cls.driver.find_element_by_link_text(value)
        elif type == "partial":
            elem = cls.driver.find_element_by_partial_link_text(value)

        else: # 如果不是上面的定位方式，则手动触发异常
            raise  ValueError("请输入正确的定位方式")
        return  elem

    @classmethod
    def sendKeys(cls,elem,text):
        '''封装输入方法'''
        return elem.send_keys(text)

    @classmethod
    def click(cls,elem):
        '''封装点击操作'''
        return elem.click()

    @classmethod
    def wait(cls,sec):
        '''封装一个隐式等待'''
        return cls.driver.implicitly_wait(sec)

    @classmethod
    def sleep(cls,sec):
        return sleep(sec)

    @classmethod
    def frame(cls,elem):
        '''切换iframe框'''
        return cls.driver.switch_to.frame(elem)

    @classmethod
    def outframe(cls):
        '''退出iframe狂'''
        return cls.driver.switch_to.default_content()

    @classmethod
    def refresh(cls):
        '''封装刷新浏览器操作'''
        return cls.driver.refresh()

    @classmethod
    def get_text(cls,elem):
        '''封装一个根据网页元素拿到text值'''
        value = BasePage.find_element(elem).text
        return value
if __name__ == '__main__':
    from selenium import webdriver
    driver = webdriver.Chrome()
    BasePage.set_driver(driver)
    driver = BasePage.get_driver()
    driver.get("http://www.baidu.com")
    driver.maximize_window()
    baidu_input = ('id', "chat-textarea")
    elem = BasePage.find_element(baidu_input)
    BasePage.sendKeys(elem,'dcs65')











































